统计学的Python实现

您所在的位置:网站首页 interquartile range怎么求 统计学的Python实现

统计学的Python实现

2024-05-24 12:42| 来源: 网络整理| 查看: 265

作者:长行

时间:2019.03.10

四分位距:四分位距(interquartile range),是一种衡量一组数据离散程度的统计量,用IQR表示。其值为第一四分位数和第三四分位数的差距。

四分位距的计算公式如下: I Q R = Q 3 − Q 1 IQR=Q_3-Q_1 IQR=Q3​−Q1​ 其中 Q 1 Q_1 Q1​为第一四分位数, Q 3 Q_3 Q3​为第三四分位数

实现代码 import math data_test=[1,2,3,4,5,6,7,8,9,10,11,12] # 定义测试数据 #四分位数计算方法一(相当于Excel的QUARTILE.EXC方法) def quantile_exc(data, n): if n3: return false data.sort() position = (len(data) + 1)*n/4 pos_integer = int(math.modf(position)[1]) pos_decimal = position - pos_integer quartile = data[pos_integer - 1] + (data[pos_integer] - data[pos_integer - 1])*pos_decimal return quartile #四分位数计算方法二(相当于Excel的QUARTILE.INC方法) def quantile_inc(data, n): if n3: return false data.sort() position = 1 + (len(data)-1)*n/4 pos_integer = int(math.modf(position)[1]) pos_decimal = position - pos_integer quartile = data[pos_integer - 1] + (data[pos_integer] - data[pos_integer - 1])*pos_decimal return quartile # 测试 print('IQR(1) =',quantile_exc(data_test,3)-quantile_exc(data_test,1)) print('IQR(2) =',quantile_inc(data_test,3)-quantile_inc(data_test,1))

结果



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3